package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.entity.TopicCommentInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface TopicCommentMapper {
    @Insert("INSERT INTO topic_comments(topic_id, user_id, content) VALUES(#{topicId}, #{userId}, #{content})")
    int insertComment(TopicCommentInfo comment);

    @Delete("DELETE FROM topic_comments WHERE comment_id = #{commentId}")
    int deleteCommentById(Integer commentId);

    @Select("<script>" +
            "SELECT * FROM topic_comments " +
            "<where>" +
            "   <if test='topicId != null'> topic_id = #{topicId} </if>" +
            "</where>" +
            "ORDER BY create_time DESC" +
            "</script>")
    List<TopicCommentInfo> getCommentsByTopicId(@Param("topicId") Integer topicId);

    // 新增 Mapper 方法
    @Select("<script>" +
            "SELECT topic_id AS topicId, title FROM discussion_topics " +
            "WHERE topic_id IN " +
            "<foreach collection='topicId' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Map<String, Object>> getTopicTitlesById(@Param("topicId") List<Integer> topicId);

    @Select("SELECT tc.* FROM topic_comments tc WHERE tc.user_id = #{userId}")
    List<TopicCommentInfo> getCommentsByUserId(@Param("userId") Integer userId);
}